package com.contract.commonlib.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.contract.commonlib.R;
import com.contract.commonlib.util.DisplayMetricsTool;
import com.yuan.library.selector.SelectorFactory;
import com.yuan.library.selector.SelectorShape;
import com.yuan.library.selector.Shape;


/**
 * @author zhengjun
 * @desc   带有选择器的view  可自定义选中效果 描边颜色等
 * @create at 2017/11/2 10:06
 */

@SuppressLint("AppCompatCustomView")
public class SelectorLinealaryoutView extends LinearLayout {
    private  int normalColor; //正常颜色

    private  int pressColor; //点击时颜色

    private  int strokeColor; //描边颜色

    private  int disableColor; //不可点击时颜色

    private  int selectColor; //选中时颜色

    private  int normalColorRes; //正常时图片

    private  int strokeWidth; //描边大小

    private  int corner; //圆角大小

    private  int leftTopCorner; //左上角圆角大小

    private  int leftBottomCorner; //左下角圆角大小

    private  int rightTopCorner; //右上角圆角大小

    private  int rightBottomCorner; //右下上角圆角大小

    public SelectorLinealaryoutView(Context context) {
        this(context,null);
    }

    public SelectorLinealaryoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.SelectorView);
        normalColor = t.getColor(R.styleable.SelectorView_normalColor, 0);
        pressColor = t.getColor(R.styleable.SelectorView_pressColor, 0);
        strokeColor = t.getColor(R.styleable.SelectorView_strokeColors, 0);
        disableColor = t.getColor(R.styleable.SelectorView_disableColor, 0);
        selectColor = t.getColor(R.styleable.SelectorView_selectColor, 0);
        normalColorRes = t.getResourceId(R.styleable.SelectorView_normalColorRes, 0);
        strokeWidth = (int)t.getDimension(R.styleable.SelectorView_strokeWidth, DisplayMetricsTool.getInstance().dip2px(context,0));
        corner = (int)t.getDimension(R.styleable.SelectorView_corner, DisplayMetricsTool.getInstance().dip2px(context,0));

        leftTopCorner = (int)t.getDimension(R.styleable.SelectorView_leftTopCorner, DisplayMetricsTool.getInstance().dip2px(context,0));
        leftBottomCorner = (int)t.getDimension(R.styleable.SelectorView_leftBottomCorner, DisplayMetricsTool.getInstance().dip2px(context,0));
        rightTopCorner = (int)t.getDimension(R.styleable.SelectorView_rightTopCorner, DisplayMetricsTool.getInstance().dip2px(context,0));
        rightBottomCorner = (int)t.getDimension(R.styleable.SelectorView_rightBottomCorner, DisplayMetricsTool.getInstance().dip2px(context,0));
        t.recycle();
        setDrawable();
    }

    private void setDrawable() {
        if (corner != 0){
            Drawable drawable = SelectorFactory.create(new SelectorShape.SelectorBuilder()
                    .normalColor(normalColor)//正常颜色
                    .pressColor(pressColor)//选中时颜色
                    .disableColor(disableColor)
                    .selectColor(selectColor)
                    .normalColorRes(normalColorRes)
                    .shapeBuilder(new Shape.ShapeBuilder().corner(corner)//圆角
                            .strokeColor(strokeColor)//描边颜色
                            .strokeWidth(strokeWidth)).build());//描边大小
            this.setBackgroundDrawable(drawable);
        }else{
            Drawable drawable = SelectorFactory.create(new SelectorShape.SelectorBuilder()
                    .normalColor(normalColor)//正常颜色
                    .pressColor(pressColor)//选中时颜色
                    .disableColor(disableColor)
                    .selectColor(selectColor)
                    .normalColorRes(normalColorRes)
                    .shapeBuilder(new Shape.ShapeBuilder()
                            .leftBottomCorner(leftBottomCorner)//圆角
                            .leftTopCorner(leftTopCorner)
                            .rightBottomCorner(rightBottomCorner)
                            .rightTopCorner(rightTopCorner)
                            .strokeColor(strokeColor)//描边颜色
                            .strokeWidth(strokeWidth)).build());//描边大小
            this.setBackgroundDrawable(drawable);
        }
    }

    public void setStrokeColor(int color){
        strokeColor = color;
        setDrawable();
    }

    /**
     *@desc  設置正常按鈕的顏色
     *@author zhengjun
     *@created at 2018/8/29 16:05
    */
    public void setNormalColor(int color){
        normalColor = color;
        setDrawable();
    }
    public void setPressColor(int color){
        pressColor = color;
        setDrawable();
    }

    /**
     *@desc  設置不可點擊時按鈕的顏色
     *@author zhengjun
     *@created at 2018/8/29 16:05
    */
    public void setDisableColor(int color){
        disableColor = color;
        setDrawable();
    }
}
